<html>
<head>
    <link href="${ctx}/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/wangEditor/3.1.1/wangEditor.min.css" rel="stylesheet">
    <link href="${ctx}/css/index.css" rel="stylesheet">
    <script src="${ctx}/jquery/2.1.4/jquery.min.js"></script>
    <script src="${ctx}/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="${ctx}/wangEditor/10.0.13/wangEditor.min.js"></script>
    <script src="${ctx}/js/iw.js"></script>
    <style>
        [class*='col-xs-'] {
            margin-bottom: 10px;
        }

        .hide {
            display: none;
        }
    </style>

</head>
<body class="container-fuild" style="padding-top: 8px;">
<form class="form-horizontal" id="form">
    <div class="col-xs-6">
        <div class="form-group">
            <label for="title" class="col-sm-3 control-label">应用名称</label>
            <div class="col-sm-9">
                <input type="text" style="display:none;" id="taskId" name="taskId"
                       value="<#if soso??>${soso.taskId}</#if>"/>
                <input type="text" class="form-control" id="title" name="title" onChange="change()" placeholder="填入应用名称"
                       value="<#if soso??>${soso.title}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="icon" class="col-sm-3 control-label">应用图标</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="icon" name="icon" placeholder="填入图标链接地址"
                       value="<#if soso??>${soso.icon}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="packagename" class="col-sm-3 control-label">应用包名</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="packagename" name="packagename" placeholder="填入应用包名"
                       value="<#if soso??>${soso.packagename}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">应用大小</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="size" name="size" placeholder="填入应用包大小，需含单位"
                       value="<#if soso??>${soso.size}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">应用市场</label>
            <div class="col-sm-9">
                <select class="form-control" id="appStore" name="appStore" onChange="change()">
                    <option value="">请选择应用市场</option>
                    <#list appStores as appStore>
                        <option value="${appStore.code}"
                                <#if soso?? && soso.appStore == appStore.code>selected</#if>><#if appStore.name??>${appStore.name}<#else>${appStore.code}</#if></option>
                    </#list>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">&nbsp;&nbsp;关键词&nbsp;&nbsp;</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="keywords" name="keywords" onChange="change()"
                       placeholder="填入应用关键词" value="<#if soso??>${soso.keywords}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">市场排名</label>
            <div class="col-sm-9">
                <input type="number" class="form-control" id="rank" name="rank" onChange="change()"
                       placeholder="填入应用在市场排名" value="<#if soso??>${soso.rank}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">试玩时间</label>
            <div class="col-sm-9">
                <input type="number" class="form-control" id="minutes" name="minutes" onChange="change()"
                       placeholder="填入应用试玩时间，单位：分钟" value="<#if soso??>${soso.minutes}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">任务总量</label>
            <div class="col-sm-9">
                <input type="number" class="form-control" id="total" name="total" placeholder="填入任务总量，填0表示不设总量"
                       value="<#if soso??>${soso.total}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">每日限量</label>
            <div class="col-sm-9">
                <input type="number" class="form-control" id="amount" name="amount" placeholder="填入每日限量，填0表示不限量"
                       value="<#if soso??>${soso.amount}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">客户名称</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="customer" name="customer" placeholder="填入客户名称，不填表示自营"
                       value="<#if soso??>${soso.customer}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">客户单价</label>
            <div class="col-sm-9">
                <input type="number" class="form-control" id="increase" name="increase"
                       placeholder="当价格等于0时不可选择“按比例”奖励模式" value="<#if soso??>${soso.increase}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">是否审核</label>
            <div class="col-sm-9">
                <select class="form-control" id="needCheck" name="needCheck">
                    <#list booleans as b>
                        <option value="${b}" <#if soso?? && soso.needCheck == b>selected</#if>>${b.title}</option>
                    </#list>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">奖励模式</label>
            <div class="col-sm-9">
                <select class="form-control" id="reward" name="reward">
                    <#list rewards as reward>
                        <option value="${reward}"
                                <#if soso?? && soso.reward == reward>selected</#if>>${reward.title}</option>
                    </#list>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">奖励类型</label>
            <div class="col-sm-9">
                <select class="form-control" id="type" name="type">
                    <#list types as type>
                        <option value="${type}" <#if soso?? && soso.type == type>selected</#if>>${type.title}</option>
                    </#list>
                </select>
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">展示奖励</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="label" name="label" placeholder="填入展示奖励，如：奖励0.9元"
                       value="<#if soso??>${soso.label}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">计算奖励</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="worth" name="worth" placeholder="随机奖励格式n:m，其他填n，单位：分"
                       value="<#if soso??>${soso.worth}</#if>">
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label for="size" class="col-sm-3 control-label">视频指南</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="media" name="media" placeholder="填入soso引导指南链接地址"
                       value="http://p5vcsyy4d.bkt.clouddn.com/video_soso_task.mp4">
            </div>
        </div>
    </div>
    <div class="col-xs-11 col-xs-offset-1">
        <input type="text" style="display:none" id="contents" name="contents"/>
        <div class="col-sm-12" id="content">
        </div>
    </div>

    <div class="col-xs-11 col-xs-offset-1">
        <input type="text" style="display:none" id="images" name="images"/>
        <div id="imgs"></div>
    </div>
</form>
<div class="col-xs-2" style="text-align: center;width: 100%">
    <button style="width: 85px;" class="btn btn-success" id="save_btn">发布</button>
</div>
<script>
    var content = "<p>复制关键词“keywords”，打开“appStore”在appStore中搜索“keywords”，找到“title”（大概在第rank位）下载完应用试玩minutes分钟，完成后来领钱。</p><p style='color:red;'>提示：任务过程中请勿关闭本任务详情页面，否则将领不到奖励。</p>";

    var E = window.wangEditor
    var editor = new E('#content')
    editor.customConfig.uploadImgShowBase64 = true;
    editor.create();
    editor.txt.html(<#if soso??>'${soso.contents}'
    <#else>content
    </#if>)
    ;

    var change = function () {
        var s = content;
        if ($("#keywords").val()) {
            s = s.replace(/keywords/g, $("#keywords").val());
        }
        if ($("#title").val()) {
            s = s.replace(/title/g, $("#title").val());
        }
        if ($("#appStore").val()) {
            s = s.replace(/appStore/g, $("#appStore option:selected").text());
        }
        if ($("#rank").val()) {
            s = s.replace(/rank/g, $("#rank").val());
        }
        if ($("#minutes").val()) {
            s = s.replace(/minutes/g, $("#minutes").val());
        }
        editor.txt.html(s);
    };

    $(function () {

        var ctx = uploadify({
            container: '#imgs'
        });

        <#if soso??>
        ctx.setContent(JSON.parse('${soso.images}'));
        </#if>

        $("#save_btn").click(function () {
            if (!$("#title").val()) {
                alert("请填入应用名称！");
                return false;
            }
            if (!$("#icon").val()) {
                alert("请填入应用图标！");
                return false;
            }
            if (!$("#packagename").val()) {
                alert("请填入应用包名！");
                return false;
            }
            if (!$("#size").val()) {
                alert("请填入应用包大小！");
                return false;
            }
            if (!$("#appStore").val()) {
                alert("请选择应用市场！");
                return false;
            }
            if (!$("#keywords").val()) {
                alert("请填入应用关键词！");
                return false;
            }
            if (!$("#rank").val()) {
                alert("请填入应用市场排名！");
                return false;
            }
            if (!$("#minutes").val()) {
                alert("请填入试玩时间！");
                return false;
            }
            if (!$("#total").val()) {
                alert("请填入任务总量！");
                return false;
            }
            if (!$("#amount").val()) {
                alert("请填入每日限量！");
                return false;
            }
            if (!$("#increase").val()) {
                $("#increase").val(0)
            }
            if (!$("#label").val()) {
                alert("请填入展示奖励！");
                return false;
            }
            if ($("#reward").val() == 'Random') {
                if (!/:|：/.test($("#worth").val())) {
                    alert("格式为n：m或者n:m，请填入正确的格式！");
                    return false;
                }
            } else {
                if (!/^[1-9]\d*$/.test($("#worth").val())) {
                    alert("请填入计算奖励需正整数！");
                    return false;
                }
            }
            $("#contents").val(editor.txt.html());
            $("#images").val(JSON.stringify(ctx.images()));
            $.ajax({
                type: "post",
                url: "/soso/save",
                data: $("#form").serialize(),
                success: function (res) {
                    alert(res);
                }
            });
        });

    });
</script>
</body>
</html>
